package com.hzhr.iot.meterplan.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import com.hzhr.iot.meterplan.constant.Meterplan_ErrorsEnum;
import com.hzhr.iot.meterplan.dao.mapper.generator.RuhujihuaEntityMapper;
import com.hzhr.iot.meterplan.dao.mapper.generator.YongqidizhiEntityMapper;
import com.hzhr.iot.meterplan.dao.mapper.generator.custom.RuhujihuaEntityCustomMapper;
import com.hzhr.iot.meterplan.dao.mapper.generator.custom.YongqidizhiEntityCustomMapper;
import com.hzhr.iot.meterplan.entity.YongqidizhiEntity;
import com.hzhr.iot.meterplan.entity.ext.RuhujihuaEntityExt;
import com.hzhr.iot.meterplan.entity.ext.YongqidizhiEntityExt;
import com.hzhr.iot.meterplan.model.meterGasUser.MeterGasUserGetModel;
import com.hzhr.iot.meterplan.model.meterGasUser.MeterGasUserModel;
import com.hzhr.iot.meterplan.model.meterGasUser.MeterGasUserPostModel;
import com.hzhr.iot.meterplan.model.meterPlan.MeterPlanModel;
import com.hzhr.iot.meterplan.model.meterPlan_meterGasUser.MeterPlan_MeterGasUserGetModel;
import com.hzhr.iot.meterplan.model.meterPlan_meterGasUser.MeterPlan_MeterGasUserPostModel;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@CygService(entity = YongqidizhiEntity.class, model = MeterGasUserModel.class)
public class MeterPlan_MeterGasUserService {

    @Autowired
    RuhujihuaEntityCustomMapper ruhujihuaEntityCustomMapper;

    @Autowired
    RuhujihuaEntityMapper ruhujihuaEntityMapper;

    @Autowired
    YongqidizhiEntityCustomMapper yongqidizhiEntityCustomMapper;

    @Autowired
    YongqidizhiEntityMapper yongqidizhiEntityMapper;


    //region javadoc注释 : post
    /**
     * 创建抄表计划下的抄表用户 <br/><br/>
     * 1.查询抄表计划，如果抄表计划不存在，抛出异常 <br/>
     * 2.如果抄表计划的状态为执行中，抛出异常“该抄表计划正在执行中，无法加入或移除用户” <br/>
     * 3.将addCodes所对应的燃气用户加入抄表计划中 <br/>
     * 这些要加入的燃气用户必须满足以下规则，才允许加入到抄表计划： <br/>
     * 如果燃气用户在某个抄表计划下，那么该抄表计划的状态不能为“执行中” <br/>
     * 4.如果delCodes所对应的燃气用户在该抄表计划下，则将这些燃气用户从这些抄表计划下移除 <br/>
     * 5.返回信息 <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/><br/>
     * @param meterPlanId 抄表计划id <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * METERPLAN_METERGASUSERSERVICE_POST_202 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 查询抄表计划状态<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 只有非执行中的抄表计划才能加入或移除用户<br/>
     * METERPLAN_METERGASUSERSERVICE_POST_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 查询抄表计划<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 抄表计划不存在<br/>
     * @see MeterPlan_MeterGasUserPostModel body <br/>
     * @see MeterPlan_MeterGasUserGetModel result <br/>
     */ 
    //endregion
    //region 说明 : post
    //创建抄表计划下的抄表用户
    //1.查询抄表计划，如果抄表计划不存在，抛出异常
    //2.如果抄表计划的状态为执行中，抛出异常“该抄表计划正在执行中，无法加入或移除用户”
    //3.将addCodes所对应的燃气用户加入抄表计划中
    //这些要加入的燃气用户必须满足以下规则，才允许加入到抄表计划：
    //如果燃气用户在某个抄表计划下，那么该抄表计划的状态不能为“执行中”
    //4.如果delCodes所对应的燃气用户在该抄表计划下，则将这些燃气用户从这些抄表计划下移除
    //5.返回信息
    //endregion
    //region 参数 : post
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "meterPlanId", value = "抄表计划id",required = true, dataType = "string", paramType = "path"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    //endregion
    //region 请求 : post
    @RequestMapping(value = "/meter-plans/{meterPlanId}/meter-gas-users", method = RequestMethod.POST)
    //endregion
    public MeterPlan_MeterGasUserGetModel post(Map<String, String> header, String meterPlanId,
                                               Map<String, Object> query, MeterPlan_MeterGasUserPostModel body) throws ServiceException{

        /* 1.查询抄表计划，如果抄表计划不存在，抛出异常201 */
        RuhujihuaEntityExt ruhujihuaEntityExts_1 = null;
        ruhujihuaEntityExts_1 = ruhujihuaEntityCustomMapper.selectRuhujihuaAndmeterPlanId(meterPlanId);
        if (ruhujihuaEntityExts_1 == null){
            throw new ServiceException(Meterplan_ErrorsEnum.METERPLAN_METERGASUSERSERVICE_POST_201);
        }

        /* 2.如果抄表计划的状态为执行中，抛出异常“该抄表计划正在执行中，无法加入或移除用户” */
        if ("1".equals(ruhujihuaEntityExts_1.getRhZhuangtai())){ // 2,待执行 3,暂停
            throw new ServiceException(Meterplan_ErrorsEnum.METERPLAN_METERGASUSERSERVICE_POST_202);
        }


        /* 3.将addCodes所对应的燃气用户加入抄表计划中
            这些要加入的燃气用户必须满足以下规则，才允许加入到抄表计划：
            如果燃气用户在某个抄表计划下，那么该抄表计划的状态不能为“执行中”*/
        //查询对象ENESYSLDC7.YONGQIDIZHI（抄表用户）中的用户号集合
        List<YongqidizhiEntityExt> yongqidizhiEntityExts_1 = null;  //对应的燃气用户
        List<String> addCodesspc = body.getAddCodes(); //用户号集合
        if (addCodesspc != null) {
            for (String addCodessp : addCodesspc) {
                yongqidizhiEntityExts_1 = yongqidizhiEntityCustomMapper.selctYongqidizhiEntityExtByZh(addCodessp);

                //将addCodes所对应的燃气用户加入抄表计划中
                //将对应的燃气用户加入抄表计划中
                //   YONGQIDIZHI.YQDZ_CBC_ID = RUHUJIHUE.RH_ID;
                if (yongqidizhiEntityExts_1 != null) {
                    for (YongqidizhiEntityExt yongqidizhiEntityExt_2 : yongqidizhiEntityExts_1) {
                        String rhZhunagtai = yongqidizhiEntityExt_2.getRhZhuangtai();
                        if ("1".equals(rhZhunagtai)) continue;
                        yongqidizhiEntityExt_2.setYqdzCbcId(ruhujihuaEntityExts_1.getRhId()); //燃气用户的抄表册计划ID（301）
                        //调用通用更新方法
                        yongqidizhiEntityMapper.updateByPrimaryKeySelective(yongqidizhiEntityExt_2);
                    }
                }

            }

        }

        /* 4.如果delCodes所对应的燃气用户在该抄表计划下，则将这些燃气用户从这些抄表计划下移除；*/
        //   YONGQIDIZHI.YQDZ_CBC_ID = RUHUJIHUE.RH_ID;
        List<String> delCodesz = body.getDelCodes();   //代码集合(移除);用户号集合(数组)
        List<YongqidizhiEntityExt> yongqidizhiEntityExts_2 = null;  //delCodes所对应的燃气用户
        if (delCodesz != null) {
            for (String delCodeszs : delCodesz) {
                yongqidizhiEntityExts_2 = yongqidizhiEntityCustomMapper.selctYongqidizhiEntityExtByZh(delCodeszs);

                //循环取出yongqidizhiEntityExts_2
                if (yongqidizhiEntityExts_2 != null) {
                    for (YongqidizhiEntityExt yongqidizhiEntityExt_3 : yongqidizhiEntityExts_2) {
                        String cbcIds = yongqidizhiEntityExt_3.getYqdzCbcId();  //delCodes燃气用户的抄表册计划ID（301）
                        //如果抄表册计划ID = RH_ID；说明delCodes所对应的燃气用户在该抄表计划下
                        if (cbcIds.equals(ruhujihuaEntityExts_1.getRhId())) {
                            //那么；则将这些燃气用户从这些抄表计划下移除；
                            yongqidizhiEntityExt_3.setYqdzCbcId("");
                            //调用通用更新方法
                            yongqidizhiEntityMapper.updateByPrimaryKeySelective(yongqidizhiEntityExt_3);

                        }
                    }
                }
            }
        }

        /* 5.返回信息 */
        MeterPlan_MeterGasUserGetModel result = new MeterPlan_MeterGasUserGetModel();
        return result;
    }


}
